#!/bin/sh
# Set keymap and xkeymap

usage() {
cat<< EOF
vkmapset - set keymap for X11 and hopefully the console too
usage: vxkbset 

EOF
}

vdir=`dirname $0`

. $vdir/vasm-functions

check_root

XORG_LIST=/etc/X11/xkb/rules/xorg.lst
XORG_CONF=/etc/X11/xorg.conf
XORG_VESA=/etc/X11/xorg.conf-vesa
KMAP_CONF=/etc/rc.d/rc.keymap

# var we want to set
KEY_MODEL=""
KEY_LAYOUT=""

## Map from xkb list (/etc/X11/xkb) to kmap (/usr/share/kbd/keymaps) 
## Somebody complete this this please
## The fields  xkb : kmap : Description
xkb_kbd() {
cat <<EOF
us:us:U.S. English
en_US::U.S. English w/ ISO9995-3
us_intl::U.S. English w/ deadkeys
al::Albanian
ar::Arabic
am::Armenian
az::Azerbaijani
by:by:Belarusian
be:be-latin1:Belgian
ben::Bengali
bs::Bosnian
br:br-latin1-us:Brazilian
bg:bg:Bulgarian
mm::Burmese
ca::Canadian
ca_enhanced:cf:French Canadian
hr:croat:Croatian
hr_US::Croatian (US)
cz:cz-cp250:Czech
cz_qwerty::Czech (qwerty)
dk:dk:Danish
nl:nl:Dutch
nl2:nl2:Dutch
dvorak:dvorak:Dvorak
dz::Dzongkha / Tibetan
ee::Estonian
fo::Faroese
fi:fi:Finnish
fr:fr:French
fr-latin9:fr-latin0:French (alternative)
ge_la::Georgian (latin)
ge_ru::Georgian (russian)
de:de:German
el:gr-pc:Greek
guj::Gujarati
gur::Gurmukhi
dev::Hindi
hu:hu:Hungarian
hu_qwerty::Hungarian (qwerty)
is::Icelandic
iu::Inuktitut
ir::Iranian
il:il-heb:Israeli
it:it:Italian
jp:jp106:Japanese
kan::Kannada
ko::Korean
lo::Lao
la::Latin America
lt::Lithuanian qwerty "numeric"
lt_std:lt:Lithuanian azerty standard
lv::Latvian
mk:mk:Macedonian
ml::Malayalam
mt::Maltese
mt_us::Maltese (US layout)
mn::Mongolian
no:no:Norwegian
ogham::Ogham
ori::Oriya
pl:pl:Polish
pl2:pl2:Polish (qwertz)
pt:pt-latin1:Portuguese
ro:ro:Romanian
ru:ru:Russian
se_FI:se-fi-lat6:Northern Saami (Finland)
se_NO::Northern Saami (Norway)
se_SE::Northern Saami (Sweden)
sr::Serbian (Cyrillic)
yu::Serbian (Latin)
si:slovene:Slovenian
sk:sk-prog-qwertz:Slovak
sk_qwerty:sk-prog-qwertz:Slovak (qwerty)
es:es:Spanish
se:se-latin1:Swedish
fr_CH:fr_CH:Swiss French
de_CH::Swiss German
syr::Syriac
tj::Tajik
tml::Tamil
tel::Telugu
th::Thai (Kedmanee)
th_tis::Thai (TIS-820.2538)
th_pat::Thai (Pattachote)
tr:tralt:Turkish
tr_f:tr_f-latin5:Turkish (F)
ua:ua:Ukrainian
gb:uk:United Kingdom
uz::Uzbek
vn::Vietnamese
nec/jp::PC-98xx Series
EOF
}

set_kmap() {

KEYMAP=`xkb_kbd | egrep "^$KEY_LAYOUT:" | cut -f2 -d :` # xkb_kbd is just an internal array ??  ... where is $KEY_LAYOUT coming from?
if [ "$KEYMAP" ]; then
cat << EOF > $KMAP_CONF
#!/bin/sh
# /etc/rc.d/rc.keymap
# Made by vxkbset on `date '+%a %b %e %Y'`
# This file is called from: /etc/rc.d/rc.M

if [ -x /usr/bin/loadkeys ]; then

  # You may load another keyboard keymap e.g. 'loadkeys fr' = french etc.
  # The keymaps are in the /usr/lib/kbd/keymaps/i386 directory.
  loadkeys $KEYMAP

  # This is so you can open other consoles using the left-[Alt] & [Up]-arrow key.
  echo "alt keycode 103 = Spawn_Console" | /usr/bin/loadkeys
fi

EOF
    chmod +x /etc/rc.d/rc.keymap
    /etc/rc.d/rc.keymap &> /dev/null
else
    $vdir/vkmapset
fi
}

set_xkb()
{
    if [ "$KEY_VARIANT" = "basic" ]; then
	KEY_VARIANT=""
    fi
   ## Change KEYBOARD_MAP
   sed -i \
   "s!.*## KEYBOARD_MAP!    Option      \"XkbLayout\" \"$KEY_LAYOUT\"    ## KEYBOARD_MAP!" \
   $1
   ## Change KEYBOARD_MODEL
   sed -i \
   "s!.*## KEYBOARD_MODEL!    Option      \"XkbModel\" \"$KEY_MODEL\"    ## KEYBOARD_MODEL!" \
   $1
   sed -i \
   "s!.*## KEYBOARD_VARIANT!    Option      \"XkbVariant\" \"$KEY_VARIANT\"    ## KEYBOARD_VARIANT!" \
   $1
   
}

##################################################################
# SET KEY MODEL
build_menu_modelx()
{
    STATE=2
    while read LINE; do
	case $STATE in
	    2)
	    if [[ "$LINE" =~ "^! *model *$" ]] ; then
		STATE=1
	    fi
	    ;;
	    1)
	    if [[ "$LINE" =~ "^ *[[:alnum:]]* *[[:alpha:]]" ]]; then
		STATE=0
		KNAME=`echo $LINE | cut -f1 -d ' '`
		KDESC=`echo $LINE | cut -f2- -d ' '`
		echo "'$KNAME'" "'$KDESC'" '\' 
	    else
		break
	    fi
	    ;;
	    0)
	    if [[ "$LINE" =~ "^ *[[:alnum:]]* *[[:alnum:]]" ]]; then
		KNAME=`echo $LINE | cut -f1 -d ' '`
		KDESC=`echo $LINE | cut -f2- -d ' '`
		echo "'$KNAME'" "'$KDESC'" '\' 
	    else
		break    
	    fi
	    ;;
	esac
    done
    return $STATE
}

menu_modelx() # This function gets called first
{
TITLE="SET KEYBOARD MODEL"
TEXT="\n
This wizard set the keyboard model and layout for X-Window and,
if possible, for text console as well. Please select your keyboard model:"
DIMENSION="20 76 11"

    echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
    if [ "$CMD" ]; then
	echo '--separator " " \' >> $fmenu
    fi
    echo '--menu "$TEXT" $DIMENSION \' >> $fmenu
    if ! build_menu_modelx < $XORG_LIST >> $fmenu ; then
	msgbox "Cannot find X11 keymap list" "ERROR"
	return 1
    fi 
    echo '2> $freply' >> $fmenu

    . $fmenu
        
    status=$?
    [ $status != 0 ] && return $status

    KEY_MODEL=`cat $freply`
    return 0
}

##################################################################
# SET KEY LAYOUT

# get directly from xorg.lst 
build_menu_layoutx1()
{
    STATE=2
    while read LINE; do
	case $STATE in
	    2)
	    if [[ "$LINE" =~ "^! *layout *$" ]] ; then
		STATE=1
	    fi
	    ;;
	    1)
	    if [[ "$LINE" =~ "^ *[[:alnum:]]* *[[:alpha:]]" ]]; then
		STATE=0
		KNAME=`echo $LINE | cut -f1 -d ' '`
		KDESC=`echo $LINE | cut -f2- -d ' '`
		echo "'$KNAME'" "'$KDESC'" '\' 
	    else
		break
	    fi
	    ;;
	    0)
	    if [[ "$LINE" =~ "^ *[[:alnum:]]* *[[:alnum:]]" ]]; then
		KNAME=`echo $LINE | cut -f1 -d ' '`
		KDESC=`echo $LINE | cut -f2- -d ' '`
		echo "'$KNAME'" "'$KDESC'" '\' 
	    else
		break    
	    fi
	    ;;
	esac
    done
    return $STATE
}


# from the xkb_kbd
build_menu_layoutx()
{
    STATE=2
    while read LINE; do
	KNAME=`echo $LINE | cut -f1 -d ':'`
	KDESC=`echo $LINE | cut -f3- -d ':'`
	echo "'$KNAME'" "'$KDESC'" '\' 
	STATE=0
    done
    return $STATE
}

menu_layoutx()
{
TITLE="SELECT KEYBOARD LAYOUT"
TEXT="\n
Please select your keyboard layout:"
DIMENSION="20 76 11"

    echo '$WCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
    if [ "$CMD" ]; then
	echo '--separator " " \' >> $fmenu
    fi
    echo '--menu "$TEXT" $DIMENSION \' >> $fmenu
    if ! xkb_kbd | build_menu_layoutx >> $fmenu ; then
	msgbox "Cannot find X11 keymap list" "ERROR"
	return 1
    fi
    echo '2> $freply' >> $fmenu

    . $fmenu
        
    status=$?
    [ $status != 0 ] && return $status

    KEY_LAYOUT=`cat $freply`
    return 0
}

menu_variantx()
{
TITLE="SELECT KEYBOARD VARIANT"
TEXT="\n
Please select your keyboard variant:"
DIMENSION="12 60 3"

    echo '$WCMD --backtitle "$BACKTITLE" --title "$TITLE" \' > $fmenu
    if [ "$CMD" ]; then
	echo '--separator " " \' >> $fmenu
    fi
    echo '--menu "$TEXT" $DIMENSION \' >> $fmenu
    echo '"basic" "Basic variant"  \' >> $fmenu
    echo '"nodeadkeys" "Eliminate dead keys" \' >> $fmenu
    echo '"bksl" "Enhanced backslash definition" \' >> $fmenu
    echo '2> $freply' >> $fmenu

    . $fmenu
        
    status=$?
    [ $status != 0 ] && return $status

    KEY_VARIANT=`cat $freply`
    return 0
}

menu_finish()
{
    infobox "Setting up keyboard ... DONE"
    [ -f $XORG_CONF ] && set_xkb $XORG_CONF
    [ -f $XORG_VESA ] && set_xkb $XORG_VESA
    ##lets send this off to /etc/sysconfig
    echo "XKBMAP=\"$KEY_LAYOUT\"" >> /etc/sysconfig/vector
    echo "XKBMODEL=\"$KEY_MODEL\"" >> /etc/sysconfig/vector
    echo "XKBVAR=\"$KEY_VARIANT\"" >> /etc/sysconfig/vector
    sleep 2
    if [ "$DISPLAY" ]; then
      setxkbmap -rules xorg -model "$KEY_MODEL" -layout "$KEY_LAYOUT" \
        -variant "$KEY_VARIANT" &> /dev/null
    fi
    set_kmap
    clean_exit 0
}

#################################################################
# Main program

wizard menu_modelx menu_layoutx menu_variantx menu_finish
clean_exit $?
